package com.sc.study.controller;

import com.sc.study.client.SysConfigClient;
import com.sc.study.dto.NoteQueryDTO;
import com.sc.study.entity.StudyNote;
import com.sc.study.service.StudyNoteService;
import com.sc.study.vo.EchartPieVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 学习笔记控制器
 *
 * @author 徐赫阳
 * @date 2021/09/14
 */
@RestController
@Api(description = "学习笔记controller")
@RequestMapping("note")
public class StudyNoteController {

    @Autowired
    StudyNoteService studyNoteService;

    @Autowired
    SysConfigClient client;


    @ApiOperation("保存笔记")
    @PostMapping("/saveNote")
    public Integer saveNote(@RequestBody StudyNote studyNote, HttpServletRequest request) throws Exception{
        System.out.println(request.getSession(true).getAttribute("user"));
        if(studyNote.getId() == null){
            studyNote.setCreatorId((Integer)request.getSession(true).getAttribute("user"));
        }else{
            studyNote.setUpdaterId((Integer)request.getSession(true).getAttribute("user"));
        }
        studyNoteService.saveOrUpdate(studyNote);
        return studyNote.getId();
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/deleteNote/{id}")
    public void deleteNote(@PathVariable Integer id){
        studyNoteService.removeById(id);
    }

    @ApiOperation("获取笔记列表")
    @PostMapping("/getNoteList")
    public List<StudyNote> getNoteList(@RequestBody NoteQueryDTO queryDTO){
        return studyNoteService.getNoteList(queryDTO);
    }

    @ApiOperation("获取更多笔记")
    @PostMapping("/getMoreNote")
    public List<StudyNote> getMoreNote(@RequestBody NoteQueryDTO queryDTO){
        return studyNoteService.getMoreNote(queryDTO);
    }

    @ApiOperation("获取echart数据")
    @GetMapping("/getEchartData")
    public List<EchartPieVO> getEchartData(){
        return studyNoteService.getEchartData()
                .stream()
                .map(val->{
                    val.setName((String) client.getDictName("EL_TYPE_DICT",val.getName()).getData());
                    return val;
                })
                .collect(Collectors.toList());
    }

    @ApiOperation("获取月笔记统计")
    @GetMapping("/getDateMap/{date}")
    public List<Map<String,String>> getDateMap(@PathVariable String date){
        if(date == null || date == ""){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
            date = sdf.format(new Date());
        }
        String year = date.substring(0,date.indexOf('-'));
        String month = date.substring(date.indexOf('-') +1,date.lastIndexOf('-'));
        return studyNoteService.getDateMap(year,month);
    }

    @ApiOperation("获取月笔记统计")
    @GetMapping("/getNoteByDate/{date}")
    public List<StudyNote> getNoteByDate(@PathVariable String date){
        return studyNoteService.getNoteByDate(date);
    }

}
